package game.dinowar;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;

import android.content.Context;

public class Player implements InterfaceSprite {

	private BitmapTextureAtlas player_BitmapTextureAtlas;
	private TiledTextureRegion player_TiledTextureRegion;
	private AnimatedSprite player_AnimatedSprite;
	private float positionX = 0;
	private float positionY = 0;

	private final int MOVE_UP = 0;
	private final int MOVE_DOWN = 1;
	private final int MOVE_LEFT = 2;
	private final int MOVE_RIGHT = 3;
	private int STATUS_PLAYER;

	public void onLoadResources(Engine mEngine, Context mContext) {
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/Player/");
		this.player_BitmapTextureAtlas = new BitmapTextureAtlas(512, 512,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		// cat hinh thanh cac dong va cot
		this.player_TiledTextureRegion = BitmapTextureAtlasTextureRegionFactory
				.createTiledFromAsset(this.player_BitmapTextureAtlas, mContext,
						"player.png", 0, 0, 2, 2);
		mEngine.getTextureManager().loadTexture(this.player_BitmapTextureAtlas);

	}

	public void onLoadScene(Scene mScene) {
		player_AnimatedSprite = new AnimatedSprite(this.positionX,
				this.positionY, this.player_TiledTextureRegion);
		// showPlayerStatus();
		mScene.attachChild(player_AnimatedSprite);
	}

	public void setPositionX(float positionX) {
		Player.this.positionX = positionX;
	}

	public float getPositionX() {
		return Player.this.positionX;
	}

	public void setPositionY(float positionY) {
		Player.this.positionY = positionY;
	}

	public float getPositionY() {
		return Player.this.positionY;
	}

	public void setPositionXY(float positionX, float positionY) {
		Player.this.positionX = positionX;
		Player.this.positionY = positionY;
	}

	public void moveUp() {
		if (STATUS_PLAYER != MOVE_UP)
			this.setStatusPlayer(MOVE_UP);
		else
			this.positionY = this.getPositionY() - 40;
		movePlayer();
	}

	public void moveDown() {
		if (STATUS_PLAYER != MOVE_DOWN)
			this.setStatusPlayer(MOVE_DOWN);
		else
			this.positionY = this.getPositionY() + 40;
		movePlayer();
	}

	public void moveLeft() {
		if (STATUS_PLAYER != MOVE_LEFT)
			this.setStatusPlayer(MOVE_LEFT);
		else
			this.positionX = this.getPositionX() - 40;
		movePlayer();
	}

	public void moveRight() {
		if (STATUS_PLAYER != MOVE_RIGHT)
			this.setStatusPlayer(MOVE_RIGHT);
		else
			this.positionX = this.getPositionX() + 40;
		movePlayer();
	}

	public void moveXY(float moveX, float moveY) {
		this.positionX = moveX;
		this.positionY = moveY;
		movePlayer();
	}

	private void movePlayer() {
		player_AnimatedSprite.setPosition(this.positionX, this.positionY);
	}

	public void setStatusPlayer(int statusplayer) {
		Player.this.STATUS_PLAYER = statusplayer;

		showPlayerStatus();
	}

	private void showPlayerStatus() {
		switch (this.STATUS_PLAYER) {
		case 0:
			player_AnimatedSprite.animate(new long[] { 100, 100 }, new int[] {
					0, 0 }, 2);
			break;
		case 1:
			player_AnimatedSprite.animate(new long[] { 100, 100 }, new int[] {
					1, 1 }, 2);
			break;
		case 2:
			player_AnimatedSprite.animate(new long[] { 100, 100 }, new int[] {
					2, 2 }, 2);
			break;
		case 3:
			player_AnimatedSprite.animate(new long[] { 100, 100 }, new int[] {
					3, 3 }, 2);
			break;
		default:
			break;
		}

	}
}
